package com.moneycollect.webspider.common;

import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;

/**
 * @author xuhuiqian
 * @since 2021/12/6 11:57
 */
public class GetHtmlHelper {

    public static String getHomePageContentByHttpClient(String url) {
        HttpGet request = new HttpGet(url);
        request.addHeader("accept", "*");
        request.addHeader("referer", url);
        request.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(20000)
                .setConnectTimeout(20000)
                .setSocketTimeout(30000)
                .build();
        request.setConfig(requestConfig);

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(request)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return getHomePageContentByHttpClient2(url);
        }

        return null;
    }

    private static String getHomePageContentByHttpClient2(String url) {
        try {
            HttpClient httpClient = HttpClient.newBuilder()
                    .connectTimeout(Duration.of(600, ChronoUnit.SECONDS))
                    .build();

            URI uri = new URI(url);
            HttpRequest request = HttpRequest.newBuilder()
                    .GET()
                    .uri(uri)
                    .header("accept", "*")
                    .header("referer", url)
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
                    .timeout(Duration.of(1200, ChronoUnit.SECONDS))
                    .build();

            var rsContent = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
            return rsContent;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getUrlHtmlContentByCdp(String url) {
        var command = new ArrayList<String>();
        command.add("--headless");//不显示google 浏览器
        Launcher launcher = new Launcher();
        try (SessionFactory factory = launcher.launch(command);
             Session session = factory.create()) {
            session.navigate(url);
            session.waitDocumentReady(60000);
            return session.getContent();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            launcher.kill();
        }
    }
}
